
const util = require('../../utils/util.js');

Page({
   data: {
      data: [],         // 接口返回参数
      databody: null,   // 文章正文
      comments: [],     // 评论

      winHeight: 0,     // 设备高度

      // 弹窗
      modalHidden: true,
      modalValue: null,

      /**
       * 分享配置
       */
      shareShow: 'none',
      shareOpacity: {},
      shareBottom: {},

   },

   // 页面初始化
   onLoad: function (options) {

      // options 为页面跳转所带来的参数
      var that = this
      var id = options.id;

      // 请求内容数据
      util.AJAX("news/" + id, function (res) {

         var arr = res.data;
         console.log("返回参数：" + JSON.stringify(arr));

         // 正文内容（需要二次处理）
         var body = arr.body;
         body = body.match(/<p>.*?<\/p>/g);
         var ss = [];
         for (var i = 0, len = body.length; i < len; i++) {

            ss[i] = /<img.*?>/.test(body[i]);

            if (ss[i]) {
               body[i] = body[i].match(/(http:|https:).*?\.(jpg|jpeg|gif|png)/);
            } else {
               body[i] = body[i].replace(/<p>/g, '')
                  .replace(/<\/p>/g, '')
                  .replace(/<strong>/g, '')
                  .replace(/<\/strong>/g, '')
                  .replace(/<a.*?\/a>/g, '')
                  .replace(/&nbsp;/g, ' ')
                  .replace(/&ldquo;/g, '"')
                  .replace(/&rdquo;/g, '"');
            }
         }

         // 输出：处理后的正文内容
         for (var i = 0; i < body.length; i++) {
            console.log(i + " " + body[i]);
         }

         // console.log("正文内容：" + body);

         // 数据绑定
         that.setData({
            data: arr,     // 接口返回数据
            databody: body // 正文内容
         });

      });

      // 请求评论
      util.AJAX("story/" + id + "/short-comments", function (res) {

         var arr = res.data.comments;

         for (var i = 0, len = arr.length; i < len; i++) {
            arr[i]['times'] = util.getTime(arr[i].time);
         }

         // 重新写入数据
         that.setData({
            comments: arr
         });

      });

      // 获取系统信息
      wx.getSystemInfo({

         success: function (res) {
            that.setData({
               winWidth: res.windowWidth,
               winHeight: res.windowHeight
            });
         }
      });

   },

   // 显示分享
   showShare: function (e) {

      // 创建动画
      var animation = wx.createAnimation({
         duration: 100,
         timingFunction: "ease",
      })
      this.animation = animation;

      var that = this;
      that.setData({
         shareShow: "block",
      });

      setTimeout(function () {
         that.animation.bottom(0).step();
         that.setData({
            shareBottom: animation.export()
         });
      }.bind(this), 400);

      // 遮罩层
      setTimeout(function () {
         that.animation.opacity(0.3).step();
         that.setData({
            shareOpacity: animation.export()
         });
      }.bind(this), 400);

   },

   /**
    * 关闭分享
    */
   shareClose: function () {
      // 创建动画
      var animation = wx.createAnimation({
         duration: 0,
         timingFunction: "ease"
      })
      this.animation = animation;

      var that = this;

      setTimeout(function () {
         that.animation.bottom(-210).step();
         that.setData({
            shareBottom: animation.export()
         });
      }.bind(this), 500);

      setTimeout(function () {
         that.animation.opacity(0).step();
         that.setData({
            shareOpacity: animation.export()
         });
      }.bind(this), 500);

      setTimeout(function () {
         that.setData({
            shareShow: "none",
         });
      }.bind(this), 1500);
   },

   /**
    * 点击分享图标弹出层
    */
   modalTap: function (e) {
      var that = this;
      that.setData({
         modalHidden: false,
         modalValue: e.target.dataset.share
      })
   },

   /**
    * 关闭弹出层
    */
   modalChange: function (e) {
      var that = this;
      that.setData({
         modalHidden: true
      })
   },

   onReady: function () {
      // 页面渲染完成 修改页面标题
      wx.setNavigationBarTitle({
         title: this.data.data.title
      })
   }
})